package guigu.TCP;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 实现TCP的网络编程
 * 例子2：客户端发送文件给服务端，服务端将文件保存在本地,并返回"发送成功"给客户端，并关闭相应的连接。
 */
public class TCPTest3 {
    //客户端
    @Test
    public void client(){
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        try {
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inet, 7878);
            os = socket.getOutputStream();
            fis = new FileInputStream("微信图片_20220917155829.jpg");
            byte buffer[] = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1)
            {
                os.write(buffer,0,len);
            }
            //关闭数据的输出
            socket.shutdownOutput();
            //接收来自服务器端的数据，并显示到控制台上
            InputStream is = socket.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte bufferr[] = new byte[20];
            int len1;
            while ((len1=is.read(bufferr))!=-1)
            {
                baos.write(bufferr,0,len1);
            }
            System.out.println(baos);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis!=null)
            {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os!=null)
            {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket!=null)
            {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //服务端
    @Test
    public void server(){
        ServerSocket socket = null;
        Socket accept = null;
        InputStream inputStream = null;
        FileOutputStream fos = null;
        try {
            socket = new ServerSocket(7878);
            accept = socket.accept();
            inputStream = accept.getInputStream();
            fos = new FileOutputStream("微信图片_20220917155829server2.jpg");
            byte buffer[] = new byte[1024];
            int len;
            while ((len=inputStream.read(buffer))!=-1)
            {
                fos.write(buffer,0,len);
            }
            //服务端给予客户端反馈
            OutputStream os = accept.getOutputStream();
            os.write("你好，我已接收到你的照片，很帅".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos!=null)
            {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(inputStream!=null)
            {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(accept!=null)
            {
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket!=null)
            {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
